//
//  SNSPostModel.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-7-30.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "SNSPostModel.h"
#import "NSDate+util.h"



@implementation SNSPostModel

+(instancetype)SNSPostWithDictionary:(NSDictionary *)dictionary{
    return [[SNSPostModel alloc]initWithDictionary:dictionary];
}

-(instancetype)initWithDictionary:(NSDictionary *)dictionary{
    if (self) {
        
        if ([dictionary[kType] isEqualToString:@"text"]) {
            _type = SNSPostText;
        }else if([dictionary[kType] isEqualToString:@"photo"]){
            _type = SNSPostPhoto;
        }
        
        _classId = dictionary[kClassId];
        if ( ![self isNullObject: dictionary[kComments]]) {
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *dict in dictionary[kComments]) {
                [tempArray addObject:[[SNSPostCommentModel alloc]initWithDictionary:dict ]];
            }
            _comments = tempArray;
        }
     
        if (![self isNullObject: dictionary[kContent]]) {
            _content = dictionary[kContent];
        }
        
        _createDate = [NSDate instanceWithString:dictionary[kCreateDate] dateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
        _dbDatetime = [NSDate instanceWithString:dictionary[kDbDatetime] dateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        _id = dictionary[kId];
        
        if ([self isNullObject:dictionary[kIsFavorite]]) {
            _isFavorite = nil;
        }else{
            _isFavorite = [[NSNumber alloc]initWithInt:[dictionary[kIsFavorite] intValue]];
        }
        
        if ([self isNullObject:dictionary[kContent]]) {
            _content = nil;
        }else{
            _content = dictionary[kContent];
        }
        
        
        _isPlusOne = [dictionary[kIsPlusOne]boolValue];
        _kindergartenId = dictionary[kKindergartenId];
        

        
        _photo = [self isNullObject:dictionary[kPhoto]]?nil:[NSURL URLWithString:dictionary[kPhoto]];

        _photoPre = [self isNullObject:dictionary[kPhotoPre]]?nil:[NSURL URLWithString:dictionary[kPhotoPre]];
        
        if (![self isNullObject: dictionary[kPlusOneList]]) {
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *dict in dictionary[kPlusOneList]) {
                [tempArray addObject:[[SNSPlusOneModel alloc]initWithDictionary:dict]];
            }
            _plusOneList = tempArray;
        }
        
        if (![self isNullObject:dictionary[kMark]]) {
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *dict in dictionary[kMark]) {
                [tempArray addObject:[[SNSMarkModel alloc]initWithDictionary:dict]];
            }
            _markList = tempArray;
        }
        
        _senderId = dictionary[kSenderId];
        _senderName = dictionary[kSenderName];
        _userPhoto = [self isNullObject:dictionary[kUserPhoto]]?nil:dictionary[kUserPhoto];
        
        _no = [dictionary[kNo]intValue];
        
    }
    return self;
}

-(BOOL)isNullObject:(id)object{
    return [object isEqual:[NSNull null]];
}

@end


@implementation SNSPostCommentModel
-(instancetype)initWithDictionary:(NSDictionary *)dictionary{
    if (self) {
        _senderId = dictionary[kSenderId];
        _senderName = dictionary[kSenderName];
        _createDate = [NSDate instanceWithString:dictionary[kCreateDate] dateFormat:@"yyyy-MM-dd hh:mm:ss"];
        _content = dictionary[kContent];
    }
    return self;
}
@end

@implementation SNSPlusOneModel

-(instancetype)initWithDictionary:(NSDictionary *)dictionary{
    if (self) {
        _photo = [NSURL URLWithString:dictionary[kUserPhoto]];
    }
    return self;
}
@end

#define kSNSMarkPosX        @"posX"
#define kSNSMarkPosY        @"posY"
#define kSNSMarkDirection   @"direction"
#define kSNSMarkContent     @"content"
@implementation SNSMarkModel

-(instancetype)initWithDictionary:(NSDictionary *)dict{
    self = [super init];
    if (self) {
        _pos = CGPointMake([dict[kSNSMarkPosX]floatValue], [dict[kSNSMarkPosY]floatValue]);
        _markDirection = [dict[kSNSMarkDirection]intValue];
        _contentText = dict[kSNSMarkContent];
    }
    return self;
}

-(NSDictionary *)toDictionary{
    return @{kSNSMarkPosX:@(_pos.x), kSNSMarkPosY:@(_pos.y), kSNSMarkDirection:@(_markDirection), kSNSMarkContent:_contentText};
}

-(NSString *)description{
    return [NSString stringWithFormat:@"{pos = %@, text = %@, dicrection = %d", NSStringFromCGPoint(_pos), _contentText, _markDirection];
}
@end